package com.party.course.service.impl;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.date.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.party.course.mapper.CityInfoMapper;
import com.party.course.domain.CityInfo;
import com.party.course.service.ICityInfoService;

import cn.hutool.core.util.IdUtil;

/**
 * 城市编码Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-21
 */
@Service
public class CityInfoServiceImpl extends ServiceImpl<CityInfoMapper, CityInfo> implements ICityInfoService {
    @Autowired
    private CityInfoMapper cityInfoMapper;

    @Override
    public void expertData() {
        String excelFilePath = "E:\\workSpace\\bykj\\课程小程序\\需求\\省市区编码20250314对客版.xlsx"; // Excel 文件路径

        try (FileInputStream fis = new FileInputStream(excelFilePath);
             Workbook workbook = new XSSFWorkbook(fis);) {
            Sheet sheet = workbook.getSheetAt(0);
            // 遍历 Excel 行
            for (Row row : sheet) {
                // 跳过表头
                if (row.getRowNum() == 0) continue;
                //省编码
                double proCode = row.getCell(0).getNumericCellValue();
                //省名称
                String proName = row.getCell(1).getStringCellValue();
                //市编码
                double cityCode = row.getCell(2).getNumericCellValue();
                //市名称
                String cityName = row.getCell(3).getStringCellValue();
                //区编码
                double areaCode = row.getCell(4).getNumericCellValue();
                //区名称
                String areaName = row.getCell(5).getStringCellValue();

                add(CityInfo.builder()
                        .proCode(String.valueOf(proCode).replace(".0", ""))
                        .proName(proName)
                        .cityCode(String.valueOf(cityCode).replace(".0", ""))
                        .cityName(cityName)
                        .areaCode(String.valueOf(areaCode).replace(".0", ""))
                        .areaName(areaName)
                        .build());
            }

        } catch (IOException e) {
            throw new RuntimeException("Error processing the Excel file", e);
        }
    }

    /**
     * 查询城市编码
     *
     * @param id 城市编码主键
     * @return 城市编码
     */
    @Override
    public CityInfo view(String id) {
        return cityInfoMapper.selectCityInfoById(id);
    }

    /**
     * 查询城市编码列表
     *
     * @param cityInfo 城市编码
     * @return 城市编码
     */
    @Override
    public List<CityInfo> findList(CityInfo cityInfo) {
        return cityInfoMapper.selectCityInfoList(cityInfo);
    }

    /**
     * 新增城市编码
     *
     * @param cityInfo 城市编码
     * @return 结果
     */
    @Override
    public int add(CityInfo cityInfo) {
        cityInfo.setId(IdUtil.simpleUUID());
        cityInfo.setDelFlag("0");
        cityInfo.setCreateTime(DateUtil.date());
        return cityInfoMapper.insert(cityInfo);
    }

    /**
     * 修改城市编码
     *
     * @param cityInfo 城市编码
     * @return 结果
     */
    @Override
    public int updateEntity(CityInfo cityInfo) {
        cityInfo.setUpdateTime(DateUtil.date());
        return cityInfoMapper.updateById(cityInfo);
    }

    /**
     * 批量删除城市编码
     *
     * @param ids 需要删除的城市编码主键
     * @return 结果
     */
    @Override
    public int deleteIds(List<String> ids) {
        return cityInfoMapper.deleteCityInfoByIds(ids);
    }

    /**
     * 删除城市编码信息
     *
     * @param id 城市编码主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return cityInfoMapper.deleteCityInfoById(id);
    }
}
